package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 药房网
 * @author tngou@tngou.net
 *
 */
public class COM_Yaofangwang extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(COM_Yaofangwang.class);
	private String url="https://www.yaofangwang.com/detail-{}.html";

	

	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null||doc.text().indexOf("无法找到该页面")>=0)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("-");
		json.put("un",uns[uns.length-1].replaceAll(".html", "-").split("-")[0]);
		//标签
		Elements mbx = doc.getElementsByClass("breadcrumb");
		if(mbx.first()!=null)
		{
			Elements as = mbx.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size()-2; i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		//图片
		Element thumblist = doc.getElementById("thumblist");
		if(thumblist!=null)
		{		
			Elements imgs = thumblist.getElementsByTag("a");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				JSONObject rel = (JSONObject) JSON.parse(e.attr("rel"));
				
				if(rel!=null)
				{
					String src = rel.getString("largeimage");
					if(StringUtil.isNotEmpty(src))
					{
						list.add(src);
					}
				}
				
			});
			json.put("picture", list);
		}
		//价格
		Elements price = doc.getElementsByClass("v-mid");
		if(price.first()!=null)
		{	
			String value = price.first().text();	
			json.put("price", getPrice(value));
			
		}
		//类型

		//说明书
		Element instruction = doc.getElementById("spsm");
		if(instruction!=null)
		{
			JSONObject instructions=instructions(instruction);
			json.put("instructions",instructions );
			
		}
		return json;
	}
	private JSONObject instructions(Element element) {
		JSONObject json = new JSONObject(true);
		Elements dts = element.getElementsByTag("dt");
		Elements dds = element.getElementsByTag("dd");
		if(dts.size()==dds.size()) {
			for (int i = 0; i < dts.size(); i++) {
				if(StringUtil.isNotEmpty(dds.get(i).text()))
				json.put(StringUtil.cleanKey(dts.get(i).text()),StringUtil.RemoveEmpty(dds.get(i).text()));
			}
		}
		Elements trs = element.getElementsByTag("tr");
		for (Element tr : trs) {
			Elements tds = tr.getElementsByTag("td");
			if(tds.size()==2) {
				json.put(StringUtil.cleanKey(tds.get(0).text()), StringUtil.RemoveEmpty(tds.get(1).text()));
			}
		}
		
		return name_number(json);
	}

}

